package com.budwk.app.util.poiUtils;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfw.dto.FireHiddenExcelParamDaYingLuSQDTO;
import com.budwk.app.sqfw.enums.DaYingLuExcelType;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.FireHiddenExcelDaYingLuStreetBO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhangp
 * @date 2023-11-30
 */
public class DaYingLuStreetPublicSecurityExcelUtils {

    /**
     * 大营路街道社会治安excel
     *
     * @param excelParamDTO excel参数
     * @return excel链接
     */
    public static String createPublicSecurityDaYingLuSQ(FireHiddenExcelParamDaYingLuSQDTO excelParamDTO) {
        int fileIndex = excelParamDTO.getFileIndex();
        //Excle社会治安重点风险场所台账
        if (fileIndex == DaYingLuExcelType.PUBLIC_SECURITY_STANDING_BOOK.getCode()) {
            return publicSecurityStandingBook(excelParamDTO, null);
        }
        return null;
    }

    public static String publicSecurityStandingBook(FireHiddenExcelParamDaYingLuSQDTO excelParamDTO, String isCheckData) {
        Map<String, Object> keyLocationRecParamMap = Maps.newHashMap();
        keyLocationRecParamMap.put("downloadDateStr", DateUtil.format(new Date(), "yyyy-MM-dd"));
        keyLocationRecParamMap.put("county", excelParamDTO.getStreet());

        List<Map<String, Object>> dataList = Lists.newArrayList();
        int numIndex = 0;

        for (Sqfw_formSummary formSummary : excelParamDTO.getFormSummaryList()) {
            numIndex++;
            Map<String, Object> rowDataMap = Maps.newHashMap();
            rowDataMap.put("num", numIndex);//序号
            rowDataMap.put("communityStr", formSummary.getCommunity());//社区
            rowDataMap.put("fillDataStr", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd HH:mm:ss"));//巡查时间
            rowDataMap.put("a1", formSummary.getA1());//场所名称
            rowDataMap.put("a2", formSummary.getA2());//场所负责人
            rowDataMap.put("a3", formSummary.getA3());//场所地址
            rowDataMap.put("a5", formSummary.getA5());//场所类型
            rowDataMap.put("a8", formSummary.getA8());//负责人电话
            String a9 = formSummary.getA9();
            List<byte[]> a9Bytes = Lists.newArrayList();
            if (StringUtils.isNotBlank(a9)) {
                a9Bytes = CommonFormOfficeUtils.addPictureToByteList(a9, a9.split(",").length);
            }
            rowDataMap.put("a9", a9Bytes);//走访图片
            rowDataMap.put("a16", formSummary.getA16());//具体分类
            String a19 = formSummary.getA19();
            if (StringUtils.isNotBlank(a19)) {
                rowDataMap.put("a19", CommonFormOfficeUtils.addPictureToByteList(formSummary.getA19(), 1));
            }

            String a20 = formSummary.getA20();//隐患描述
            if ("无".equals(a20)) {
                rowDataMap.put("a20", formSummary.getA20());
                rowDataMap.put("a20_1", formSummary.getA20());
            } else {
                rowDataMap.put("a20", "有");
                rowDataMap.put("a20_1", formSummary.getA20());
            }
            String a68 = formSummary.getA68();//有无涉黑涉恶线索
            if (StringUtils.isNotBlank(a68) && !"无".equals(a68)) {
                rowDataMap.put("a68", "有");
                rowDataMap.put("a68_1", a68);
            } else {
                if(StringUtils.isBlank(a68)){
                    a68 = "无";
                }
                rowDataMap.put("a68", a68);
                rowDataMap.put("a68_1", a68);
            }

            dataList.add(rowDataMap);
            //zhangp 2023-12-08 检测数据时只要有一条数据就返回
            if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
                return "true";
            }
        }
        //zhangp 2023-12-04 检测数据时使用
        if (StringUtils.isNotBlank(isCheckData) && isCheckData.equals("isCheckData")) {
            if (dataList.isEmpty()) {
                return "false";
            }
        }
        String templatePath = CommonFormOfficeUtils.getTemplatePathByOS("daYingLu/publicSecurityKeyRiskAreasStandingBook.xlsx");
        //String fileName = "社会治安重点风险场所台账";
        String fileName = DownLoadFileNameEnum.DYL_PUBLIC_SECURITY_STANDING_BOOK.getName();
        if (StringUtils.isNotBlank(excelParamDTO.getFileNameSuffix()))
            fileName += excelParamDTO.getFileNameSuffix();

        ExcelFileCreateParamBO excelFileCreateParam = new ExcelFileCreateParamBO();
        excelFileCreateParam.setTemplatePath(templatePath);
        excelFileCreateParam.setSheetParam(keyLocationRecParamMap);
        excelFileCreateParam.setDataList(dataList);
        excelFileCreateParam.setClazz(FireHiddenExcelDaYingLuStreetBO.class);
        excelFileCreateParam.setFileName(fileName);
        excelFileCreateParam.setReturnWebUrlFlag(excelParamDTO.getReturnWebUrlFlag());
        excelFileCreateParam.setListNotLastRow(true);

        return CommonFormOfficeUtils.createExcelAndUpload(excelFileCreateParam);
    }

}
